﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGX.Server.Game
{
    public static class ClientPool
    {
        #region Properties

        public static Dictionary<Int64, GameClient> GameClients
        {
            get { lock (ClientPool.GameClientsLock) { return ClientPool._gameClients; } }
            set { lock (ClientPool.GameClientsLock) { ClientPool._gameClients = value; } }
        }
        private static Dictionary<Int64, GameClient> _gameClients = new Dictionary<Int64, GameClient>();
        private static object GameClientsLock = new object();

        #endregion

        #region Public Methods

        public static void AddClient(Int64 clientAccountId, GameClient client)
        {
            if (!ClientPool.GameClients.ContainsKey(clientAccountId))
                ClientPool.GameClients.Add(clientAccountId, client);
        }

        public static void RemoveClient(Int64 clientAccountId)
        {
            if (ClientPool.GameClients.ContainsKey(clientAccountId))
                ClientPool.GameClients.Remove(clientAccountId);

            // remove from any teams
            // ...
        }

        public static GameClient GetByThreadId(int threadId)
        {
            GameClient gc = null;

            foreach (var c in ClientPool.GameClients.Values)
            {
                if (c.MainThread.ManagedThreadId == threadId)
                {
                    gc = c;
                }
            }

            return gc;
        }

        #endregion
    }
}
